package cn.ike.web.controller.system;

import cn.ike.domain.system.Dept;
import cn.ike.service.system.DeptService;
import cn.ike.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Log4j
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    // 注入service
    @Autowired
    private DeptService deptService;

    /**
     * 1.部门列表分页查询
     * 请求路径：http://localhost:8080/system/dept/list
     * 响应路径：/WEB-INF/pages/system/dept/factory-list.jsp
     */
    @RequestMapping("/list")
    public ModelAndView list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        String companyId = getLoginCompanyId();
        ModelAndView model = new ModelAndView();
        try {
            log.info("执行部门列表分页查询开始...");
            PageInfo<Dept> pageInfo = deptService.findByPage(companyId, pageNum, pageSize);
            // deptPage.setSize(deptPage.getList().size());
            System.out.println("每页的数量" + pageInfo.getPageSize());
            System.out.println("当前页的数量" + pageInfo.getSize());
            model.addObject("pageInfo", pageInfo);
            model.setViewName("system/dept/dept-list");
            log.info("执行部门列表分页查询结束...");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行部门列表分页查询出现异常！", e);
        }
        return model;
    }

    /**
     * 2. 进入部门添加页面，部门数据回显
     * http://localhost:8080/system/dept/toAdd
     */
    @RequestMapping("/toAdd")
    public ModelAndView toAdd() {
        String companyId = getLoginCompanyId();
        String deptId = "100";
        ModelAndView model = new ModelAndView();
        try {
            log.info("执行部门数据回显开始...");
            List<Dept> deptList =  deptService.findAll(companyId, deptId);
            model.addObject("deptList", deptList);
            model.setViewName("system/dept/dept-add");
            log.info("执行部门数据回显结束...");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行部门数据回显出现异常！", e);
        }
        return model;
    }

    /**
     * 3. 添加或修改部门
     * http://localhost:8080/system/dept/edit
     */
    @RequestMapping("edit")
    public ModelAndView edit(Dept dept) {
        ModelAndView model = new ModelAndView();
        // 设置企业信息
        dept.setCompanyId(getLoginCompanyId());
        dept.setCompanyName(getLoginCompanyName());
        try {
            if (StringUtils.isEmpty(dept.getId())){
                log.info("执行添加部门开始...");
                deptService.save(dept);
                log.info("执行添加部门结束...");
            }else {
                log.info("执行部门信息修改结束...");
                deptService.update(dept);
                log.info("执行部门信息修改结束...");
            }
            model.setViewName("redirect:/system/dept/list");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行添加或修改部门出现异常！", e);
        }
        return model;
    }

    /**
     * 4.修改部门信息数据回显
     * http://localhost:8080/system/dept/toUpdate
     */
    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(String deptId) {
        ModelAndView model = new ModelAndView();
        try {
            log.info("执行修改部门信息数据回显开始...");
            // 根据部门id查询部门
            Dept dept = deptService.findById(deptId);
            // 根据部门id查询所有上级部门
            List<Dept> deptList = deptService.findAll(dept.getCompanyId(),deptId);
            model.addObject("dept",dept);
            model.addObject("deptList",deptList);
            model.setViewName("/system/dept/dept-update");
            log.info("执行修改部门信息数据回显结束...");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行修改部门信息数据回显出现异常！", e);
        }
        return model;
    }

    /**
     * 5.删除部门
     * http://localhost:8080/system/dept/delete
     */
    @RequestMapping("delete")
    @ResponseBody
    public Map<String, String> delete(String deptId) {
        Map<String, String> result = new HashMap<>();
        try {
            log.info("执行删除部门的开始...");
            boolean flag = deptService.delete(deptId);
            System.out.println("flag" + flag);
            if (flag){
                result.put("msg","删除成功！");
            }else {
                result.put("msg","删除失败：删除的部门有子部门，不能删除！");
            }
            log.info("执行删除部门的结束...");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行删除部门出现异常！", e);
        }
        return result;
    }
}
